package com.lzw.bluetoothpractice.Activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lzw.bluetoothpractice.MyApplication;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.TimeUtils;

public class StepRecordActivity extends BaseActivity {
    TextView tvTime;
    Button btStart;
    Button btEnd;
    TextView distance;
    TextView energy;
    TextView speed;
    TextView step;
    private boolean isStopCount = false;
    private int currentSecond = 0;//当前毫秒数
    private int startStep=0;
    private int endStep;
    private boolean isPause = false;

    private Handler mHander=new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_step_record);
        startStep= Integer.parseInt(MyApplication.getInstance().step);
        initView();
    }
    public void initView(){
        InitNavBar(true,"行走记录",true);
        distance=fd(R.id.distance);
        energy=fd(R.id.energy);
        speed=fd(R.id.speed);
        step=fd(R.id.step);
        tvTime=fd(R.id.tvTime);
        btStart=fd(R.id.btStart);
        btEnd=fd(R.id.btEnd);

        btStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isPause){
                    btStart.setText("暂停");

                    mHander.post(timeRunable);
                }else{
                    btStart.setText("开始");

                    mHander.removeCallbacks(timeRunable);
                }
                isPause=!isPause;


            }
        });
        btEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                btStart.setText("开始");
                isPause=false;
                endStep=Integer.parseInt(MyApplication.getInstance().step);
                if (endStep-startStep==0){
                    distance.setText("0");
                    step.setText("0");
                    energy.setText("0");
                    speed.setText("0");
                }else {
                    distance.setText(""+(endStep-startStep)*0.5);
                    step.setText(""+(endStep-startStep));
                    energy.setText(""+(endStep-startStep)*0.04);
                    //Log.e("速度",""+currentSecond);
                    if (currentSecond!=0){
                        speed.setText(""+((endStep-startStep)*50000)/currentSecond);
                    }else {
                        speed.setText("0");
                    }


                }

                currentSecond = 0;
                mHander.removeCallbacks(timeRunable);
            }
        });

    }

    private Runnable timeRunable = new Runnable() {
        @Override
        public void run() {

            currentSecond = currentSecond + 1000;
            endStep=Integer.parseInt(MyApplication.getInstance().step);
            if (endStep-startStep==0){
                distance.setText("0");
                step.setText("0");
                energy.setText("0");
                speed.setText("0");

            }else {
                distance.setText(""+(endStep-startStep)*0.5);
                step.setText(""+(endStep-startStep));
                energy.setText(""+(endStep-startStep)*0.04);
               // Log.e("速度",""+currentSecond);
                speed.setText(""+((endStep-startStep)*50000)/currentSecond);

            }
            tvTime.setText(TimeUtils.getFormatHMS(currentSecond));
            //递归调用本runable对象，实现每隔一秒一次执行任务
            mHander.postDelayed(this, 1000);

        }

    };
}
